package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import models.Exercicio;
import models.GrupoMuscular;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import dao.DAOExercicio;
import dao.DAOGrupoMuscular;

@Security.Authenticated(Seguranca.class)
public class ExercicioControle extends Controller {

	private static Form<Exercicio> formExercicio = new Form<Exercicio>(Exercicio.class);
	  
    public static Result index() {
    	return listarView("", "");
    }
	
	public static Result cadastrarView(){
		
		DAOGrupoMuscular dao = new DAOGrupoMuscular();
		try {
			Set<GrupoMuscular> grupos = dao.select();
			return ok(views.html.exercicio.cadastrar.render(grupos));
		} catch (Exception e) {
			return listarView("alert-error", e.getMessage());
		}

	}
	
	public static Result editarView(int id){
		DAOExercicio dao = new DAOExercicio();
		DAOGrupoMuscular daoG = new DAOGrupoMuscular();
		try{
			Set<GrupoMuscular> grupos = daoG.select();
		
			Exercicio exercicio = new Exercicio();
			exercicio = dao.select(id);
			return ok(views.html.exercicio.editar.render(exercicio, grupos));
		}catch(Exception e){
			return listarView("alert-error", e.getMessage());
		}
	}
	
	public static Result listarView(String tipoAlerta, String mensagem){
		DAOExercicio dao = new DAOExercicio();
		try {
			return ok(views.html.exercicio.listar.render(dao.select(), tipoAlerta, mensagem));
		} catch (Exception e) {
			return ok(views.html.exercicio.listar.render(null, tipoAlerta, e.getMessage() +"<br/>"+ mensagem));
		}
	}
	
	public static Result cadastrar(){
		Form<Exercicio> novoExercicio = formExercicio.bindFromRequest();
		Exercicio exercicio = novoExercicio.get();

		try{
			exercicio.cadastrar();
			return listarView("alert-success", "Exercício cadastrado com sucesso.");
		} catch (Exception e) {
			return listarView("alert-error", e.getMessage());
		}
	}
	
	public static Result editar(){
		Form<Exercicio> novoExercicio = formExercicio.bindFromRequest();
		Exercicio exercicio = novoExercicio.get();
		
		try{
			exercicio.editar();
			return listarView("alert-success", "Exercício editado com sucesso.");
		} catch (Exception e) {
			return listarView("alert-error", e.getMessage());
		}
		
	}
	
	public static Result remover(){
		Form<Exercicio> novoExercicio = formExercicio.bindFromRequest();
		Exercicio exercicio = novoExercicio.get();
		try{
			exercicio.remover();
			return listarView("alert-success", "Exercício removido com sucesso.");
		}catch(Exception e){
			return listarView("alert-error", e.getMessage());
		}
	}
	
	public static Result buscarExercicio(String nome) {
		DAOExercicio dao = new DAOExercicio();
		try {
			List exercicio = new ArrayList<Exercicio>(dao.selectXNomeEGrupoMuscular(nome));
			return ok(views.html.exercicio.exercicioJson.render(exercicio));
		} catch (Exception e) {
			return ok("[\"grupoMuscular\":{\"id\":\"-1\"}]");
		}
	}
	
}
